package com.heima.kafka.listener;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojo.User;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author 胡慧
 * @title: HelloListener
 * @projectName heima-leadnews
 * @description:消息消费者，监听
 * @date 2021/2/2417:32
 */
@Component
public class HelloListener {
    @KafkaListener(topics = {"kafka-hello"})
    public void receiverMessage(ConsumerRecord<?,?> record){
        //判断非空
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if (optional.isPresent()){
            Object value = record.value();
            User user = JSON.parseObject((String) value, User.class);
            System.out.println(user);
        }
    }
}
